import { getToken } from "./auth";

const baseUrl = 'http://localhost:1001/api/';

/**
 * @param {string} url   
 * @param {string} method
 * @param {object}  data
 * @param {object} header    
 */
const request = ({url,method,data,header}) => {
	header = {
		...header,
		Authorization:`Bearer ${getToken()}`,
	};
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method: method || 'GET',
			data: data || {},
			header:header || {},
			success: (res) => {
				if (res.statusCode === 200) {
					resolve(res.data);
				}
				if (res.statusCode === 401){
					uni.reLaunch({
						url:'/pages/login/login'
					})
				}
			},
			fail: (err) => {
				reject(new Error('request error !'));
			}
		})
	});
};

export default request;